package com.foreveross.crawl.vo;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.foreveross.GlobalMapOperatorBeta1;
import com.foreveross.crawl.dto.TaskStatisticsDto;

/** 
 * @ClassName: TaskStatisticsVO 
 * @Description: TODO
 * @author luofangyi
 * @date 2014-10-11 下午3:48:36 
 *  
 */ 
public class TaskStatisticsVO {

	private Long channelId;
	private String channelName;
	private String taskState;
	private Long count;
	
	public TaskStatisticsVO(Long count) {
		super();
		this.count = count;
	}

	public TaskStatisticsVO(){
		
	}

	public TaskStatisticsVO(Long channelId, String taskState, Long count) {
		this.channelId = channelId;
		this.taskState = taskState;
		this.count = count;
	}

	public TaskStatisticsVO(Long channelId, String channelName,
			String taskState, Long count) {
		super();
		this.channelId = channelId;
		this.channelName = channelName;
		this.taskState = taskState;
		this.count = count;
	}
	
	public Long getChannelId() {
		return channelId;
	}

	public Long getCount() {
		return count;
	}

	public void setCount(Long count) {
		this.count = count;
	}

	public TaskStatisticsVO(Long channelId, String taskState) {
		super();
		this.channelId = channelId;
		this.taskState = taskState;
	}

	public String getTaskState() {
		return taskState;
	}

	public void setTaskState(String taskState) {
		this.taskState = taskState;
	}

	public void setChannelId(Long channelId) {
		this.channelId = channelId;
	}

	public String getChannelName() {
		return channelName;
	}

	public void setChannelName(String channelName) {
		this.channelName = channelName;
	}

	public static List<TaskStatisticsDto> getObtainTaskBillBeta2(List<TaskStatisticsVO> taskList) {
		
		List<TaskStatisticsDto> dtoList = new ArrayList<TaskStatisticsDto>();
		Map<Long, ChannelInfo> channelDetail = GlobalMapOperatorBeta1.getAllChannelDetail();
		DecimalFormat df = new DecimalFormat("0.00");//格式化小数，不足的补0
		for(Long channelId : channelDetail.keySet()){
			TaskStatisticsDto dto = new TaskStatisticsDto();
			int total = 0;
			for(TaskStatisticsVO vo : taskList){
				if(channelId == vo.getChannelId()){
					dto.setChannelId(vo.getChannelId());
					dto.setChannelName(vo.getChannelName());
					Long count = vo.getCount();
					total += count;
					if(vo.getTaskState().equals("FINISHED")){
						dto.setTaskSuccess(count.intValue());
					} else if(vo.getTaskState().equals("DOING")){
						dto.setTaskDoing(count.intValue());
					} else if(vo.getTaskState().equals("NORUN")){
						//TODO 暂时未统计
						
					} else if(vo.getTaskState().equals("EXCEPTION")){
						dto.setTaskException(count.intValue());
					}
				}
			}
			if(dto.getChannelName() != null && !dto.getChannelName().trim().equals("")){
				dto.setTaskTotal(total);
				calculateSucessRates(df, dto);
				dtoList.add(dto);
			}
		}
		return dtoList;
	}

	public static List<TaskStatisticsDto> getObtainTaskBillBeta1() {
		Map<Long, TaskStatisticsBeta1> taskStatisticsMap = GlobalMapOperatorBeta1.getTaskStatisticsBeta1();
		Map<Long, ChannelInfo> channelDetail = GlobalMapOperatorBeta1.getAllChannelDetail();
		DecimalFormat df = new DecimalFormat("0.00");//格式化小数，不足的补0
		List<TaskStatisticsDto> dtoList = new ArrayList<TaskStatisticsDto>();
		try {
			for(Long channelId : channelDetail.keySet()){
				TaskStatisticsDto dto = new TaskStatisticsDto();
				TaskStatisticsBeta1 task = taskStatisticsMap.get(channelId);
				dto.setChannelId(channelId);
				ChannelInfo channelInfo = channelDetail.get(channelId);
				dto.setChannelName(channelInfo != null ? channelInfo.getChannelName() : "");
				if (task != null) {
					dto.setTaskTotal(task.getTaskTotal());
					dto.setTaskDoing(task.getTaskDoing());
					dto.setTaskSuccess(task.getTaskSuccess());
					dto.setTaskException(task.getTaskException());
					calculateSucessRates(df, dto);
				} else {
					dto.setSuccessRates("0");
				}
				dtoList.add(dto);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dtoList;
	}
	
	/**
	 * 合并H2及缓存中的任务统计结果
	 * @param list
	 * @return
	 */
	public static List<TaskStatisticsDto> getObtainTaskBillBeta1(List<TaskStatisticsDto> list){
		
		List<TaskStatisticsDto> retList = new ArrayList<TaskStatisticsDto>();
		DecimalFormat df = new DecimalFormat("0.00");//格式化小数，不足的补0
		for(Long channelId : GlobalMapOperatorBeta1.getAllChannel()){
			if(list != null){
				for(TaskStatisticsDto dto : list){
					if(dto.getChannelId() == channelId || dto.getChannelId().equals(channelId)){
						int taskOfDoing = GlobalMapOperatorBeta1.getTaskSizeByChannel(channelId);
						dto.setTaskDoing(taskOfDoing);
						dto.setTaskTotal(taskOfDoing + dto.getTaskTotal());
						calculateSucessRates(df, dto);
						retList.add(dto);
					}
				}
			} else {
				int taskOfDoing = GlobalMapOperatorBeta1.getTaskSizeByChannel(channelId);
				if(taskOfDoing != 0){
					TaskStatisticsDto dto = new TaskStatisticsDto();
					dto.setTaskDoing(taskOfDoing);
					dto.setTaskTotal(taskOfDoing);
					dto.setTaskException(0);
					dto.setTaskSuccess(0);
					dto.setSuccessRates("0.00");
					retList.add(dto);
				}
			}
		}
		
		return retList;
	}
	
	private static void calculateSucessRates(DecimalFormat df, TaskStatisticsDto dto) {
		if (dto.getTaskSuccess() == 0) {
			dto.setSuccessRates("0");
		} else {
			float rates = (dto.getTaskSuccess() / ((float)dto.getTaskSuccess() + (float)dto.getTaskException())) * 100;
			dto.setSuccessRates(df.format(rates));
		}
	}
}
